from research.calcor.math.updownlimit_price_stock import calc_uplimite_price,calc_downlimite_price
class isuplimit_flag_4:
    def __init__(self,r=0.09,preclose="preclose"):
        self.r=r
        self.preclose=preclose
    def oncalc(self,b,timekey=None):
        close = b["close"]
        up_price = b[self.preclose]*(1+self.r)
        if close>= up_price:
            r = 1
        else:
            r = 0
        return r
class isuplimit_flag_3:
    def __init__(self,up_price=None,preclose="preclose"):
        self.up_price = up_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["close"]
        if self.up_price is None:
            up_price = calc_uplimite_price(obj, b[self.preclose], timekey)
        if close>= up_price:
            r = 1
        else:
            r = 0
        return r
class isuplimit_flag_2:
    def __init__(self,up_price=None,preclose="preclose"):
        self.up_price = up_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["high"]
        if self.up_price   is None:
            up_price = calc_uplimite_price(obj, b[self.preclose], timekey)
        if close>= up_price and b["open"]<b["close"]:
            r = 1
        else:
            r = 0
        return r
class isuplimit_flag_1:
    def __init__(self,up_price=None,preclose="preclose"):
        self.up_price = up_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["high"]
        if self.up_price   is None:
            up_price = calc_uplimite_price(obj, b[self.preclose], timekey)
        if close>= up_price:
            r = 1
        else:
            r = 0
        return r
class isdownlimit_flag_4:
    def __init__(self,r=0.09,preclose="preclose"):
        self.r=r
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        close = b["close"]
        down_price = b[self.preclose]*(1-self.r)
        if close<= down_price:
            r = 1
        else:
            r = 0
        return r
class isdownlimit_flag_3:
    def __init__(self,down_price=None,preclose="preclose"):
        self.down_price = down_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["close"]
        if self.down_price is None:
            down_price = calc_downlimite_price(obj, b[self.preclose], timekey)
        if close<= down_price:
            r = 1
        else:
            r = 0
        return r
class isdownlimit_flag_2:
    def __init__(self,down_price=None,preclose="preclose"):
        self.down_price = down_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["high"]
        if self.down_price   is None:
            down_price = calc_downlimite_price(obj, b[self.preclose], timekey)
        if close<= down_price and b["open"]>b["close"]:
            r = 1
        else:
            r = 0
        return r
class isdownlimit_flag_1:
    def __init__(self,down_price=None,preclose="preclose"):
        self.down_price = down_price
        self.preclose=preclose

    def oncalc(self, b, timekey=None):
        timekey = b["timekey"]
        obj = b["obj"]
        close = b["high"]
        if self.down_price   is None:
            down_price = calc_downlimite_price(obj, b[self.preclose], timekey)
        if close<= down_price:
            r = 1
        else:
            r = 0
        return r